﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using Common;
using System.ComponentModel;
using GalaSoft.MvvmLight.Ioc;

namespace ViewModel.StationManager
{
    public class StationUserMapViewModel: ViewModelBase
    {
        public ICommand AutoCompleteQueryCommand { get; set; }
        public ICommand AddMapCommand { get; set; }
        public ICommand DetailCommand { get; set; }

        private string m_UserAutoCompleteInput;
        public string UserAutoCompleteInput
        {
            get { return m_UserAutoCompleteInput; }
            set { this.m_UserAutoCompleteInput = value; base.RaisePropertyChanged(() => UserAutoCompleteInput); }
        }

        private List<User> m_UserList;
        public List<User> UserList
        {
            get { return this.m_UserList; }
            set { this.m_UserList = value; base.RaisePropertyChanged(() => this.UserList); }
        }

        private IUserService m_UserService;

        public StationUserMapViewModel(IUserService userService)
        {
            this.m_UserService = userService;

            this.AutoCompleteQueryCommand = new RelayCommand(QueryUsers);
            this.AddMapCommand = new RelayCommand<User>(ShowMapStationForm);
            this.DetailCommand = new RelayCommand<User>(ShowDetailForm);

            QueryUsers();
        }

        public async void QueryUsers()
        {
            try
            {
                if (!string.IsNullOrEmpty(UserAutoCompleteInput))
                {
                    this.UserList = (await this.m_UserService.GetUsersAsync(new UserQueryCriteria
                    {
                        AutoCompleteInput = this.UserAutoCompleteInput
                    })).ToList();
                }
                else
                {
                    this.UserList = (await this.m_UserService.GetUsersAsync()).ToList();
                }
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("用户信息加载失败 ", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }

        public void ShowMapStationForm(User user)
        {
            StationUserMapSettingViewModel vm = SimpleIoc.Default.GetInstanceWithoutCaching<StationUserMapSettingViewModel>();
            vm.User = user;
            this.MessengerInstance.Send<ViewModelBase>(vm, "StationUserMap_ShowMapSettingForm");
        }

        public void ShowDetailForm(User user)
        {
            StationUserMapDetailViewModel vm = SimpleIoc.Default.GetInstanceWithoutCaching<StationUserMapDetailViewModel>();
            vm.User = user;
            this.MessengerInstance.Send<ViewModelBase>(vm, "StationUserMap_ShowMapDetailForm");
        }
    }
}
